package org.blueoxygen.cimande2.ma.gene.manager;

import java.util.ArrayList;

import org.blueoxygen.cimande2.ma.gene.entity.Preferences;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbManager extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "preference.db";
	private static final int DATABASE_VERSION = 1;
	public static final String TABLE = "data";
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String CONNECTION = "connection";
	public SQLiteDatabase db;

	public DbManager(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public ArrayList<Preferences> getDataList() {
		db = getReadableDatabase();
		Cursor cursor = db.query(TABLE, null, null, null, null, null, null);
		new Activity().startManagingCursor(cursor);
		ArrayList<Preferences> preferences = new ArrayList<Preferences>();
		while (cursor.moveToNext()) {
			Preferences preference = new Preferences(cursor.getString(0),
					cursor.getString(1), cursor.getString(2));
			preferences.add(preference);
		}
		return preferences;
	}
	
	public String getConnectionUrl(){
		db = getReadableDatabase();
		Cursor cursor = db.query(TABLE, null, null, null, null, null, null);
		new Activity().startManagingCursor(cursor);
		String connection="";
		while (cursor.moveToNext()) {
			connection = cursor.getString(2);
		}
		db.close();
		return connection;
	}
	public String getUsername(){
		db = getReadableDatabase();
		Cursor cursor = db.query(TABLE, null, null, null, null, null, null);
		new Activity().startManagingCursor(cursor);
		String connection="";
		while (cursor.moveToNext()) {
			connection = cursor.getString(0);
		}
		db.close();
		return connection;
	}

	public void addData(String username, String password, String connection) {
		SQLiteDatabase db = getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(DbManager.USERNAME, username);
		values.put(DbManager.PASSWORD, password);
		values.put(DbManager.CONNECTION, connection);
		db.insert(DbManager.TABLE, null, values);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table " + TABLE + "( " + USERNAME
				+ " text not null, " + PASSWORD + " text not null, "
				+ CONNECTION + " text not null);";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion >= newVersion)
			return;

		String sql = null;
		if (oldVersion == 1)
			sql = "alter table " + TABLE + " add note text;";
		if (oldVersion == 2)
			sql = "";

		Log.d("EventsData", "onUpgrade	: " + sql);
		if (sql != null)
			db.execSQL(sql);
	}
}
